#pragma once

#include "GlobalIncludes.h"
#include "Msg.h"
#include <time.h>

#define PROB (((float)rand())/((float)RAND_MAX))

class gameManager
{
private:
public:
	gameManager()
	{

	}

	int init()
	{
		srand(timeGetTime());
		return 0;
	}

	msg clientInfo(msg newMsg)
	{
		newMsg.year++;
		newMsg.bushels -= newMsg.buysell*newMsg.bpa;
		newMsg.bushels -= newMsg.feed;
		newMsg.bushels -= newMsg.plant;

		float c = ((float)newMsg.pop);
		float a = c * 20.0f;
		float p = (((float)newMsg.feed)/a);
		float s = c * ((p<1.0f)?p:1.0f);
		newMsg.starved = newMsg.pop - ((int)s);
		newMsg.pop = newMsg.pop - newMsg.starved;

		float rnd = PROB;
		newMsg.plague = (rnd<0.15f)?true:false;
		newMsg.pop = (newMsg.plague)?newMsg.pop/2:newMsg.pop;

		float r = PROB;
		float n = r*16.0f;
		newMsg.newbs = (int)((r>0.5f)?abs(n):0.0f);
		newMsg.pop = newMsg.pop+newMsg.newbs;

		newMsg.acres = newMsg.acres+newMsg.buysell;
		newMsg.farmed = (rand()%5)+1;
		newMsg.bushels += newMsg.farmed*newMsg.acres;

		int v = (rand()%5)+1;
		newMsg.rats = (v!=2||v!=4)?v/newMsg.bushels:0;
		newMsg.bushels -= newMsg.rats;

		newMsg.bpa = (rand()%11)+17;

		return newMsg;
	}

	msg newGame()
	{
		msg gameState;
		gameState.year	  = 1;
		gameState.starved = 0;
		gameState.plague  = 0;
		gameState.pop     = 100;
		gameState.newbs   = 5;
		gameState.acres   = 1000;
		gameState.farmed  = 3;
		gameState.rats    = 200;
		gameState.bushels = 2800;
		gameState.bpa     = (rand()%11)+17;
		gameState.buysell = 0;
		gameState.feed    = 0;
		gameState.plant   = 0;
		return gameState;
	}
};